package com.ruoyi.system.service.impl;

import java.rmi.server.ServerCloneException;
import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysAttendanceRecord;
import com.ruoyi.system.mapper.SysAttendanceRecordMapper;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysFamilyMapper;
import com.ruoyi.system.domain.SysFamily;
import com.ruoyi.system.service.ISysFamilyService;

/**
 * 家属管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-21
 */
@Service
public class SysFamilyServiceImpl extends ServiceImpl<SysFamilyMapper,SysFamily> implements ISysFamilyService
{
    @Autowired
    private SysFamilyMapper sysFamilyMapper;

    @Autowired
    private ISysUserService userService ;

    /**
     * 查询家属管理
     * 
     * @param id 家属管理主键
     * @return 家属管理
     */
    @Override
    public SysFamily selectSysFamilyById(Long id)
    {
        return sysFamilyMapper.selectSysFamilyById(id);
    }

    /**
     * 查询家属管理列表
     * 
     * @param sysFamily 家属管理
     * @return 家属管理
     */
    @Override
    public List<SysFamily> selectSysFamilyList(SysFamily sysFamily)
    {
        if (SecurityUtils.hasRole("parent")) {
            sysFamily.setParentId(SecurityUtils.getUserId());
        }
        if (SecurityUtils.hasRole("common")){
            sysFamily.setChildId(SecurityUtils.getUserId());
        }
        return sysFamilyMapper.selectSysFamilyList(sysFamily);
    }

    /**
     * 新增家属管理
     * 
     * @param sysFamily 家属管理
     * @return 结果
     */
    @Override
    public int insertSysFamily(SysFamily sysFamily)
    {
        if (ObjectUtils.isEmpty(sysFamily.getChildId())) {
            throw new ServiceException("家属编号不能为空!") ;
        }
        sysFamily.setCreateTime(DateUtils.getNowDate());
        sysFamily.setParentId(SecurityUtils.getUserId());
        SysUser sysUser = userService.selectUserById(sysFamily.getChildId());
        sysFamily.setName(sysUser.getNickName());

        return sysFamilyMapper.insertSysFamily(sysFamily);
    }

    /**
     * 修改家属管理
     * 
     * @param sysFamily 家属管理
     * @return 结果
     */
    @Override
    public int updateSysFamily(SysFamily sysFamily)
    {
        sysFamily.setUpdateTime(DateUtils.getNowDate());
        return sysFamilyMapper.updateSysFamily(sysFamily);
    }

    /**
     * 批量删除家属管理
     * 
     * @param ids 需要删除的家属管理主键
     * @return 结果
     */
    @Override
    public int deleteSysFamilyByIds(Long[] ids)
    {
        return sysFamilyMapper.deleteSysFamilyByIds(ids);
    }

    /**
     * 删除家属管理信息
     * 
     * @param id 家属管理主键
     * @return 结果
     */
    @Override
    public int deleteSysFamilyById(Long id)
    {
        return sysFamilyMapper.deleteSysFamilyById(id);
    }
}
